$(function () {
    //变量抽取
    var attence_datagrid = $("#attence_datagrid");
    attence_datagrid.datagrid({
        url: '/attendance/list',
        toolbar: "#toolbar",
        striped: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        sortOrder: 'asc',
        pagination: true,
        pagePosition: "bottom",
        fit: true,
        columns: [[
            {field: 'staff', title: '名字', width: 100, align: 'center', width: 100,
                formatter: function (value) {
                    return value ? value.name : '';
                }},
            {field: "inTime", title: '上班', align: 'center', width: 100},
            {field: "outTime", title: '下班', align: 'center', width: 100},
            {
                field: "inState", title: '签到状态', align: 'center', width: 100, formatter: function (value, row) {
                switch (row.inState) {
                    case 1:
                        //非正常
                        return "<span style='color: green'>正常</span>";
                        break;
                    case 2:
                        //正常
                        return "<span style='color: red'>迟到</span>";
                        break;
                    default:
                }
            }
            },
            {
                field: "outState", title: '签退状态', align: 'center', width: 100, formatter: function (value, row) {
                //console.log(row);
                switch (row.outState) {
                    case 1:
                        //非正常
                        return "<span style='color: green'>正常</span>";
                        break;
                    case 2:
                        //正常
                        return "<span style='color: red'>早退</span>";
                        break;
                    default:
                }
            }
            }
        ]]
    });

    var methodObj = {
        //刷新
        reload: function reload() {
            attence_datagrid.datagrid("load");
        },
        //高级查询
        query: function query() {
            var attence_month = $("#attence_month").val();
            attence_datagrid.datagrid('load', {
                month: attence_month
            });
        }
    };
    //调用方法
    $('a[data-cmd]').click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    })
});

$(function () {
    var attence_month = $("#attence_month");
    attence_month.datebox({
        //显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
        onShowPanel: function () {
            //触发click事件弹出月份层
            span.trigger('click');
            if (!tds)
            //延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                setTimeout(function () {
                    tds = p.find('div.calendar-menu-month-inner td');
                    tds.click(function (e) {
                        //禁止冒泡执行easyui给月份绑定的事件
                        e.stopPropagation();
                        //得到年份
                        var year = /\d{4}/.exec(span.html())[0],
                            //月份
                            //之前是这样的month = parseInt($(this).attr('abbr'), 10) + 1;
                            month = parseInt($(this).attr('abbr'), 10);

                        //隐藏日期对象
                        attence_month.datebox('hidePanel')
                        //设置日期的值
                            .datebox('setValue', year + '-' + month);
                    });
                }, 0);
        },
        //配置parser，返回选择的日期
        parser: function (s) {
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },
        //配置formatter，只返回年月 之前是这样的d.getFullYear() + '-' +(d.getMonth());
        formatter: function (d) {
            var currentMonth = (d.getMonth() + 1);
            var currentMonthStr = currentMonth < 10 ? ('0' + currentMonth) : (currentMonth + '');
            return d.getFullYear() + '-' + currentMonthStr;
        }
    });

    //日期选择对象
    var p = attence_month.datebox('panel'),
        //日期选择对象中月份
        tds = false,
        //显示月份层的触发控件
        span = p.find('span.calendar-text');
    var curr_time = new Date();
    //设置前当月
    attence_month.datebox("setValue", formatterDate(curr_time));

    //格式化日期
    function formatterDate(date) {
        //获取年份
        var y = date.getFullYear();
        //获取月份
        var m = date.getMonth() + 1;
        return y + '-' + m;
    }
});
























